

[!NOTE]
This is one of 208 standalone projects, maintained as part
of the @thi.ng/umbrella monorepo
and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on
GitHub. Thank you! ❤️
About
Sparse & bitwise adjacency matrices, lists and selected traversal algorithms for directed & undirected graphs.
[!IMPORTANT]
In July 2024 this package was restructured and split-up to extract some
features into smaller more focused packages:
Graph implementations
The following types all implement the IGraph
interface and
support both directed & undirected graphs:
Traversals
Status
STABLE - used in production
Search or submit any issues for this package
Related packages
- @thi.ng/dgraph - Type-agnostic directed acyclic graph (DAG) & graph operations
Installation
yarn add @thi.ng/adjacency
ESM import:
import * as adj from "@thi.ng/adjacency";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/adjacency"></script>
JSDelivr documentation
For Node.js REPL:
const adj = await import("@thi.ng/adjacency");
Package sizes (brotli'd, pre-treeshake): ESM: 2.53 KB
Dependencies
Note: @thi.ng/api is in most cases a type-only import (not used at runtime)
Usage examples
One project in this repo's
/examples
directory is using this package:
 | Poisson-disk shape-aware sampling, Voronoi & Minimum Spanning Tree visualization | Demo | Source |
API
Generated API docs
TODO
Basic usage
import { defAdjBitMatrix, type Edge } from "@thi.ng/adjacency";
const rels = {
a: ["b", "c"],
b: ["d"],
c: ["d", "e"],
e: ["a", "d", "b"],
};
const nodeIDs = [
...new Set(Object.entries(rels).flatMap(([id, rels]) => [id, ...rels])),
];
const index = new Map(nodeIDs.map((id, i) => [id, i]));
const edges = Object.entries(rels).flatMap(([id, rels]) =>
rels.map((x) => <Edge>[index.get(id), index.get(x)])
);
const graph = defAdjBitMatrix(nodeIDs.length, edges, true);
console.log("edges:", graph.numEdges(), "verts:", graph.numVertices());
console.log(graph.hasVertex(index.get("d")!));
console.log(graph.hasEdge(index.get("a")!, index.get("d")!));
console.log(graph.degree(index.get("a")!));
console.log(graph.neighbors(index.get("a")!).map((x) => nodeIDs[x]));
console.log(graph.toDot(nodeIDs));
graph.resize(10);
graph.addEdge(4, 5);
graph.removeEdge(0, 1);
GraphViz visualization of the above example graph:

Authors
If this project contributes to an academic publication, please cite it as:
@misc{thing-adjacency,
title = "@thi.ng/adjacency",
author = "Karsten Schmidt and others",
note = "https://thi.ng/adjacency",
year = 2018
}
License
© 2018 - 2025 Karsten Schmidt // Apache License 2.0